package com.qfedu.entity.course10;

import java.util.ArrayList;
import java.util.List;

/**
 * 邻接矩阵实现
 */
public class AdMatrixGraph {
    private List vertexList;//存储点的链表
    private int[][] edges;//邻接矩阵，用来存储边
    private int numOfEdges;//边的数目

    public AdMatrixGraph(int n) {
        // 初始化矩阵，一维数组，和边的数目
        edges = new int[n][n];
        vertexList = new ArrayList(n);
        numOfEdges = 0;
    }

    public static void main(String args[]) {
        int n = 5, e = 5;//分别代表结点个数和边的数目
        String[] labels = {"V1", "V2", "V3", "V4", "V5"};//结点的标识
        AdMatrixGraph graph = new AdMatrixGraph(n);
        for (String label : labels) {
            graph.insertVertex(label);//插入结点
        }//插入四条边
        graph.insertEdge(0, 1, 2);
        graph.insertEdge(0, 2, 5);
        graph.insertEdge(2, 3, 8);
        graph.insertEdge(3, 0, 7);
        graph.insertEdge(2, 4, 6);
        System.out.println("结点个数是：" + graph.getNumOfVertex());
        System.out.println("边的个数是：" + graph.getNumOfEdges());
    }

    //得到结点的个数
    public int getNumOfVertex() {
        return vertexList.size();
    }

    //    得到边的数目
    public int getNumOfEdges() {
        return numOfEdges;
    }//返回结点i的数据

    public Object getValueByIndex(int i) {
        return vertexList.get(i);
    }

    //返回v1,v2的权值
    public int getWeight(int v1, int v2) {
        return edges[v1][v2];
    }

    //插入结点
    public void insertVertex(Object vertex) {
        vertexList.add(vertex);
    }

    //插入边
    public void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        numOfEdges++;
    }
}
